package br.ufrj.dcc.dojo.jtrace;

import java.awt.TexturePaint;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.jtrace.Material;
import org.jtrace.Materials;
import org.jtrace.Scene;
import org.jtrace.Tracer;
import org.jtrace.ViewPlane;
import org.jtrace.cameras.Camera;
import org.jtrace.cameras.PinHoleCamera;
import org.jtrace.examples.io.SimplePLYExample;
import org.jtrace.geometry.Plane;
import org.jtrace.geometry.Sphere;
import org.jtrace.geometry.Triangle;
import org.jtrace.io.PlyReader;
import org.jtrace.lights.Light;
import org.jtrace.listeners.ImageListener;
import org.jtrace.listeners.TimeListener;
import org.jtrace.primitives.ColorRGB;
import org.jtrace.primitives.Point3D;
import org.jtrace.primitives.Vector3D;
import org.jtrace.shader.Shader;
import org.jtrace.shader.Shaders;


public class Dojo {
    public static void main(String[] args) throws IOException{
    	
    	InputStream is = SimplePLYExample.class.getResourceAsStream("scissors.ply");
		List<Triangle> triangles;
 
		triangles = PlyReader.read(is, Materials.metallic(ColorRGB.WHITE));
		Scene scene = new Scene();
		
		for (Triangle t : triangles) {
			scene.add(t);
		}
		
        ViewPlane viewPlane = new ViewPlane(1024,768);
        
        Point3D eye = new Point3D(-5,0,10);
        Point3D sphereCenter = new Point3D(10.0,0,10.0);
        Point3D lookAt = new Point3D(10.0,0,0);
        Vector3D up = new Vector3D(0,0,1.0);

        Point3D lightPoint = new Point3D(-5,-15.0,5.0);

        Camera camera = new PinHoleCamera(eye,lookAt,up);
        Material material = Materials.metallic(ColorRGB.WHITE);
        Material material2 = Materials.metallic(ColorRGB.BLUE);
        Material materialBola = Materials.metallic(ColorRGB.GREEN);

        camera.setZoomFactor(5);

        scene.setCamera(camera);
        //ImageListener image = new ImageListener("DojoImage.png", "png");

        //Sphere sphere = new Sphere(sphereCenter, 10, materialBola);
       // scene.add(sphere);

        Point3D pointPlane = new Point3D(0, 0, -40);
        Vector3D normalPlane = new Vector3D(0, 0, 1);
        Plane plane = new Plane(pointPlane, normalPlane, material);
        Plane plane2 = new Plane(pointPlane.multiply(-1), normalPlane.multiply(-1), material2);
        scene.add(plane);
        scene.add(plane2);
        Light light = new Light(lightPoint);
        scene.add(light);

        Shader shader = Shaders.ambientShader();
        Shader difusa = Shaders.diffuseShader();
        Shader especular = Shaders.specularShader(4);

        Tracer tracer = new Tracer();
        //tracer.addListeners(image);
        tracer.addShaders(shader, difusa, especular);
        tracer.addInterceptors(new ShadowIntercept());
        
        tracer.addListeners(new ImageListener("sphere.png", "png"), new TimeListener());
        tracer.render(scene, viewPlane);

    }
}
